<?php
class User_admin_model extends MY_Model{  
    function User_admin_model(){
        parent::MY_Model();
        $this->load->library('session');        
        $this->load->helper('ip','date');
        $this->user_admin_table='bur_admins';
    }
    
    function set_admin_session( $username, $logged_in, $last_login=0){
        if ($last_login == 0) $last_login = time();        
        $newdata = array(
           'admin'=> $username,
           'logged_in'=> $logged_in,
           'last_login'=> $last_login
        );
        $this->session->set_userdata($newdata);
    }
    
    function login($username='', $password=''){
        $username = trim($username);
        $password = trim($password);   
        if ($username != '' AND $password != ''){            
            $admin_login = array('username' => $username, 'password' => md5($password));
            
            $query = $this->db->get_where($this->user_admin_table, $admin_login);
			//echo $this->db->last_query();

            $admin_data = $query->row_array();
			
            if ($admin_data){                
                $this->set_admin_session($admin_data['username'], 'yes', mysql_to_unix($admin_data['last_login']));
                $data['last_login'] = date('Y-m-d H:i:s');
                $data['last_login_ip'] = encode_ip($this->input->ip_address());
                $this->db->where('id',$admin_data['id']);
                $this->db->update($this->user_admin_table,$data);
                return TRUE;
                //print_r($admin_data);
                //exit;
            }else{
                return FALSE;
            }       
        }else{
            return FALSE;
        }        
    }
    
    function unset_admin_session(){        
        $session['admin']  = '';        
        $session['logged_in'] = '';        
        $session['last_login'] = '';        
        $this->session->unset_userdata($session);
    }
}
?>
